package homework;

class MyBlockingQueue {
    private String[] array = null;
    int size = 0;
    int head = 0;
    int tail = 0;
    private Object locker = new Object();

    public MyBlockingQueue(int capacity){
        array = new String[capacity];
    }
    public void put(String elem) throws InterruptedException{
        synchronized(locker){
            while (size >= array.length) {
                locker.wait();
            }
            array[tail] = elem;
            tail++;
            if (tail >= array.length) {
                tail = 0;
            }
            size++;
            locker.notify();
        }
    }
    public String take() throws InterruptedException{
        synchronized(locker){
            while (size >= 0) {
                locker.wait();
            }
            String result = array[head];
            head++;
            if (head >= array.length) {
                head = 0;
            }
            size--;
            locker.notify();
            return result;
        }
    }
}
public class demo8 {

}
